<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Invite.php 2010-07-02 19:54 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Inviter_Plugin_Signup_Invite extends Core_Plugin_FormSequence_Abstract {
	protected $_name = 'inviter';
	
	protected $_formClass;
	
	protected $_script;
	
	protected $_adminFormClass;
	
	protected $_adminScript;
	
	protected $_skip;
	
	protected $_errors;
	
	public function __construct() {
		$session = new Zend_Session_Namespace ( 'inviter' );
		
		$this->_checkInviteCode ();
		
		// IE problem
		if (isset ( $_REQUEST ['fb_xd_fragment'] )) {
			echo '<script type="text/javascript">if (window.opener && window.opener.provider){window.opener.provider.check_fb_session();} window.close();</script>';
			exit ();
		}
		
		if (! $session->__isset ( 'inviterStep' )) {
			$this->_formClass = 'Inviter_Form_Signup_Invite';
			$this->_script = array ('signup/index.tpl', 'inviter' );
		} 

		elseif ($session->__get ( 'inviterStep' ) == 'friendRequest') {
			$this->_formClass = 'Inviter_Form_Signup_Members';
			$this->_script = array ('signup/members.tpl', 'inviter' );
		} 

		elseif ($session->__get ( 'inviterStep' ) == 'sendInvitation') {
			$this->_formClass = 'Inviter_Form_Signup_Contacts';
			$this->_script = array ('signup/contacts.tpl', 'inviter' );
		}
		
		$this->_adminFormClass = 'Inviter_Form_Admin_Signup_Invite';
		$this->_adminScript = array ('admin-signup/invite.tpl', 'inviter' );
	}
	
	public function onSubmit(Zend_Controller_Request_Abstract $request) {
		$session = new Zend_Session_Namespace ( 'inviter' );
		$skip = $request->getParam ( "skip" );
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		$form = $this->getForm ();
		
		if ($skip == "skipFormInviter") {
			$this->setActive ( false );
			$this->onSubmitIsValid ();
			$this->getSession ()->skip = true;
			$this->_skip = true;
			return true;
		} elseif (! in_array ( $skip, array ("skipFormMembers", "skipFormContacts" ) )) {
			if ($form->isValid ( $_REQUEST )) {
				
				/**
				 * @var $providerApi Inviter_Api_Provider
				 */
				$providerApi = Engine_Api::_ ()->getApi ( 'provider', 'inviter' );
				
				if ($form->getValue ( 'inviterStep' ) == 'getContacts') {
					if (true === ($result = Engine_Api::_ ()->getApi ( 'openinviter', 'inviter' )->getContacts ( $form, true )) && $session->__isset ( 'members' )) {
						$this->_form = new Inviter_Form_Signup_Members ();
						$this->_formClass = 'Inviter_Form_Signup_Members';
						$this->_script = array ('signup/members.tpl', 'inviter' );
						$session->__set ( 'inviterStep', 'friendRequest' );
						
						return false;
					} elseif ($result !== true) {
						$this->_errors ['contacts'] = $result;
					} elseif (! $session->__isset ( 'contacts' )) {
						$this->_errors ['contacts'] = $translate->translate ( "INVITER_Unable to get contacts." );
					}
				} 

				elseif ($form->getValue ( 'inviterStep' ) == 'friendRequest') {
					$user_ids = $request->getParam ( "inviterMembers" );
					if (count ( $user_ids ) == 0) {
						$this->_errors ['users'] = $translate->translate ( 'INVITER_Failed! No user selected, please try again later.' );
					} else {
						$session->__set ( 'user_ids', $user_ids );
					}
				} elseif ($form->getValue ( 'inviterStep' ) == 'inviterFinalize') {
					$contact_ids = $request->getParam ( "inviterContacts" );
					$message = $request->getParam ( "messageBox", "" );
					
					$provider = $session->__isset ( 'provider' ) ? $session->__get ( 'provider' ) : false;
					$providerApi = Engine_Api::_ ()->getApi ( 'provider', 'inviter' );
					
					if (count ( $contact_ids ) == 0) {
						$this->_errors ['contacts'] = $translate->translate ( 'INVITER_Failed! No contact selected, please try again later.' );
					} elseif ($provider == 'twitter' && $providerApi->checkIntegratedProvider ( $provider ) && ! $providerApi->checkTwitterMessageLength ( $message )) {
						$this->_errors ['contacts'] = $translate->translate ( 'INVITER_Failed! The text length of your message is over the limit.' );
					} else {
						$session->__set ( 'contact_ids', $contact_ids );
						$session->__set ( 'message', $message );
					}
				}
			} else {
				$this->_errors ['form_value'] = 'INVITER_Failed! Please check fields and try again later.';
			}
		}
		
		if (count ( $this->_errors ) > 0) {
			foreach ( $this->_errors as $error ) {
				$form->addError ( $error );
			}
			
			return false;
		} 

		elseif (in_array ( $form->getValue ( 'inviterStep' ), array ('getContacts', 'friendRequest' ) ) && $session->__isset ( 'contacts' ) && $skip != 'skipFormContacts') {
			$this->_form = new Inviter_Form_Signup_Contacts ();
			$this->_formClass = 'Inviter_Form_Signup_Contacts';
			$this->_script = array ('signup/contacts.tpl', 'inviter' );
			$session->__set ( 'inviterStep', 'sendInvitation' );
			
			return false;
		}
		
		parent::onSubmit ( $request );
	}
	
	public function onView() {
		$session = new Zend_Session_Namespace ( 'inviter' );
		
		if ($session->__isset ( 'invite_code' )) {
			// delete core invites
			$coreInvitesTbl = Engine_Api::_ ()->getDbtable ( 'invites', 'invite' );
			$coreInvitesTbl->delete ( array ("code = '{$session->__get('invite_code')}'" ) );
		}
	}
	
	public function onProcess() {
		$session = new Zend_Session_Namespace ( 'inviter' );
		$provider = $session->__isset ( 'provider' ) ? $session->__get ( 'provider' ) : false;
		$providerApi = Engine_Api::_ ()->getApi ( 'provider', 'inviter' );
		
		if (! $this->_skip && ! $this->getSession ()->skip && $session->__isset ( 'custom_submit_fb' )) {
			$contact_ids = ($session->__get ( 'contact_ids' )) ? explode ( ',', $session->__get ( 'contact_ids' ) ) : array ();
			Engine_Api::_ ()->getApi ( 'core', 'inviter' )->sendFacebookInvites ( $contact_ids, $session->__get ( 'invite_code' ) );
		} elseif (! $this->_skip && ! $this->getSession ()->skip && $providerApi->checkIntegratedProvider ( $provider )) {
			$contact_ids = ($session->__get ( 'contact_ids' )) ? explode ( ',', $session->__get ( 'contact_ids' ) ) : array ();
			
			$tokensTbl = Engine_Api::_ ()->getDbTable ( 'tokens', 'inviter' );
			
			$access_token_params = Zend_Json::decode ( $session->__get ( 'account_info' ), Zend_Json::TYPE_ARRAY );
			$token = $tokensTbl->getUserTokenByArray ( $access_token_params );
			
			if (! $token) {
				return false;
			}
			
			$providerApi->sendInvites ( $token, $provider, $contact_ids );
		} elseif (! $this->_skip && ! $this->getSession ()->skip) {
			
			Engine_Api::_ ()->getApi ( 'openinviter', 'inviter' )->sendRequests ();
			Engine_Api::_ ()->getApi ( 'openinviter', 'inviter' )->sendInvitations ();
		}
	}
	
	public function onAdminProcess($form) {
		$settings = Engine_Api::_ ()->getApi ( 'settings', 'core' );
		$step_table = Engine_Api::_ ()->getDbtable ( 'signup', 'user' );
		$step_row = $step_table->fetchRow ( $step_table->select ()->where ( 'class = ?', 'Inviter_Plugin_Signup_Invite' ) );
		$step_row->enable = $form->getValue ( 'enable' ) && ($settings->getSetting ( 'user.signup.inviteonly' ) != 1);
		$step_row->save ();
	}
	
	private function _checkInviteCode() {
		$session = new Zend_Session_Namespace ( 'inviter' );
		$settings = Engine_Api::_ ()->getApi ( 'settings', 'core' );
		
		// check account data posted
		if (! isset ( $_REQUEST ['email'] ) || ! isset ( $_REQUEST ['code'] )) {
			return;
		}
		
		// check settings
		if ($settings->getSetting ( 'user.signup.inviteonly' ) == 0) {
			return;
		}
		
		// check invite code
		if (! $session->__isset ( 'invite_code' ) || ! ($inviter_code = $session->__get ( 'invite_code' ))) {
			return;
		}
		
		/**
		 * @var $inviterTbl Inviter_Model_DbTable_Invites
		 */
		$inviterTbl = Engine_Api::_ ()->getDbTable ( 'invites', 'inviter' );
		$coreTbl = Engine_Api::_ ()->getDbtable ( 'invites', 'invite' );
		
		$inviterSel = $inviterTbl->select ()->where ( 'code = ?', $inviter_code )->where ( 'new_user_id = ?', 0 );
		
		$invites = $inviterTbl->fetchAll ( $inviterSel );
		if ($invites->count () == 0) {
			return;
		}
		
		$invite = $invites->getRow ( 0 );
		
		if (! $invite) {
			return;
		}
		
		$coreSel = $coreTbl->select ()->where ( 'code = ?', $inviter_code )->where ( 'new_user_id = ?', 0 );
		
		if ($coreTbl->fetchRow ( $coreSel )) {
			return;
		}
		
		$inviter_code = $session->__get ( 'invite_code' );
		$inviter_email = ($session->__isset ( 'invite_email' )) ? $session->__get ( 'invite_email' ) : $_REQUEST ['email'];
		
		$inviteCoreItem = $coreTbl->createRow ();
		$inviteCoreItem->setFromArray ( array ('user_id' => $invite->user_id, 'recipient' => $inviter_email, 'code' => $inviter_code, 'timestamp' => $invite->sent_date ) );
		
		$inviteCoreItem->save ();
	}
}